﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
using DevExpress.XtraReports.UI;
using DevExpress.XtraReports.UserDesigner;

namespace BHair.Report
{
    public partial class frmxsmx : WeifenLuo.WinFormsUI.Docking.DockContent
    {
        public frmxsmx()
        {
            InitializeComponent();
        }
        string constr = ConfigurationManager.ConnectionStrings["BHair.Properties.Settings.BHairDBConnectionString"].ConnectionString;
        private void frmxsmx_Load(object sender, EventArgs e)
        {
           // comboBox1.Text = "";
            yg();
        }
        public void yg()
        {
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            string sql = "SELECT [eid],[ename] FROM [BHairDB].[dbo].[employee]";
            DataSet ds = new DataSet();

            SqlDataAdapter sda = new SqlDataAdapter(sql,conn);
            sda.Fill(ds, "employee");
            comboBox1.DataSource = ds.Tables["employee"];
            comboBox1.DisplayMember = "ename";
            comboBox1.SelectedIndex = -1;
            conn.Close();
        }
        public DataSet selecttime()
        {
            string sql = string.Format("SELECT * FROM [BHairDB].[dbo].[销售明细] where 单据日期>'{0}' and 单据日期<'{1}'",dtpbegin.Text,dtpend.Text);
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            SqlDataAdapter sda = new SqlDataAdapter(sql,conn);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            dataGridView1.DataSource = dt;
            conn.Close();
            DataSet ds = new DataSet();
            sda.Fill(ds,"销售明细");
            return ds;
        }
        public DataSet selectspname()
        {
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            DataSet ds = new DataSet();
            string sql = string.Format("SELECT * FROM [BHairDB].[dbo].[销售明细] where 单据日期>'{0}' and 单据日期<'{1}' and 商品名称 like '%{2}%'",dtpbegin.Text,dtpend.Text,txtspname.Text);
            SqlDataAdapter sda = new SqlDataAdapter(sql,conn);
            DataTable dt = new DataTable();

            sda.Fill(dt);
            dataGridView1.DataSource = dt;
            sda.Fill(ds,"销售明细");
            conn.Close();
            return ds;
        }
        public DataSet selectyg()
        {
            string sql = string.Format("SELECT * FROM [BHairDB].[dbo].[销售明细] where 单据日期>'{0}' and 单据日期<'{1}' and 经手人= '{2}'", dtpbegin.Text, dtpend.Text, comboBox1.Text);
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            SqlDataAdapter sda = new SqlDataAdapter(sql,conn);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            dataGridView1.DataSource = dt;
            DataSet ds = new DataSet();
            sda.Fill(ds,"销售明细");
            conn.Close();
            return ds;

        }
        public DataSet selectall()
        {
            string sql = string.Format("SELECT * FROM [BHairDB].[dbo].[销售明细] where 单据日期>'{0}' and 单据日期<'{1}' and 经手人= '{2}' and 商品名称 like '%{3}%' ", dtpbegin.Text, dtpend.Text, comboBox1.Text,txtspname.Text);
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            SqlDataAdapter sda = new SqlDataAdapter(sql,conn);
            DataTable dt = new DataTable();

            sda.Fill(dt);
            dataGridView1.DataSource = dt;
            DataSet ds = new DataSet();
            sda.Fill(ds,"销售明细");
            conn.Close();
            return ds;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (txtspname.Text == string.Empty && comboBox1.Text == string.Empty)//根据时间查询
            {
                selecttime();
            }
            else if (txtspname.Text != string.Empty && comboBox1.Text == string.Empty)//根据时间商品名查询
            {
                selectspname();
            }
            else if (txtspname.Text == string.Empty && comboBox1.Text != string.Empty)//根据职员查询
            {
                selectyg();

            }
            else if (txtspname.Text != string.Empty && comboBox1.Text != string.Empty) //按商品名职员查询
            
            {
                selectall();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (txtspname.Text == string.Empty && comboBox1.Text == string.Empty)//根据时间查询
            {
                DevExpress.XtraReports.UI.XtraReport xr = new DevExpress.XtraReports.UI.XtraReport();
                xr.LoadLayout(AppDomain.CurrentDomain.BaseDirectory + @"bb\商品销售明细.repx");
                xr.DataSource = selecttime();
                xr.ShowPreview();
            }
            else if (txtspname.Text != string.Empty && comboBox1.Text == string.Empty)//根据时间商品名查询
            {
                DevExpress.XtraReports.UI.XtraReport xr = new DevExpress.XtraReports.UI.XtraReport();
                xr.LoadLayout(AppDomain.CurrentDomain.BaseDirectory + @"bb\商品销售明细.repx");
                xr.DataSource = selectspname();
                xr.ShowPreview();
                
            }
            else if (txtspname.Text == string.Empty && comboBox1.Text != string.Empty)//根据职员查询
            {
                DevExpress.XtraReports.UI.XtraReport xr = new DevExpress.XtraReports.UI.XtraReport();
                xr.LoadLayout(AppDomain.CurrentDomain.BaseDirectory + @"bb\商品销售明细.repx");
                xr.DataSource = selectyg();
                xr.ShowPreview();
                

            }
            else if (txtspname.Text != string.Empty && comboBox1.Text != string.Empty) //按商品名职员查询

            {
                DevExpress.XtraReports.UI.XtraReport xr = new DevExpress.XtraReports.UI.XtraReport();
                xr.LoadLayout(AppDomain.CurrentDomain.BaseDirectory + @"bb\商品销售明细.repx");
                xr.DataSource = selectall();
                xr.ShowPreview();
            }
            
        }

        private void dataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
            
        }

        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
       e.RowBounds.Location.Y,
       dataGridView1.RowHeadersWidth - 4,
       e.RowBounds.Height);

            TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
            dataGridView1.RowHeadersDefaultCellStyle.Font,
                rectangle,
                dataGridView1.RowHeadersDefaultCellStyle.ForeColor,
                TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }
    }
}
